﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceManager.DataAccessLayer
{
    public class CTKiemKeDbOperation
    {
        QLThietBiEntities devicemanager;
        #region IDatabaseOperation<T> Members

        public int SaveRecord(CTKIEMKE record)
        {
            try
            {
                using (devicemanager = new QLThietBiEntities())
                {
                    devicemanager.CTKIEMKEs.Add(record);
                    devicemanager.SaveChanges();
                }
                return (int)record.KiemKeID;
            }
            catch
            {
                return -1;
            }
        }

        public bool UpdateRecord(CTKIEMKE record)
        {
            using (devicemanager = new QLThietBiEntities())
            {
                devicemanager.THIETBIs.Find(record.CTKiemKeID);
                devicemanager.SaveChanges();
                return true;
            }
        }

        public List<CTKIEMKE> GetRecord(CTKIEMKE record)
        {
            devicemanager = new QLThietBiEntities();
            IQueryable<CTKIEMKE> custQuery = devicemanager.CTKIEMKEs.AsQueryable<CTKIEMKE>();
            if (record.KiemKeID != 0)
                custQuery = custQuery.Where(c => c.KiemKeID == record.KiemKeID);
            return custQuery.ToList();
        }

        public bool DeleteRecord(CTKIEMKE record)
        {

            using (devicemanager = new QLThietBiEntities())
            {
                var cust = devicemanager.THIETBIs.FirstOrDefault(c => c.ThietBiID == record.CTKiemKeID);
                devicemanager.THIETBIs.Remove(cust);
                devicemanager.SaveChanges();
                return true;
            }
        }

        #endregion
    }
}
